using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Model.BaseManage.Dto;
using ZR.Model.BaseManage;
using ZR.Repository;
using ZR.Service.BaseManage.IBaseManageService;
using Infrastructure;

namespace ZR.Service.BaseManage
{
    /// <summary>
    /// agv信息Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IBaseAgvService), ServiceLifetime = LifeTime.Transient)]
    public class BaseAgvService : BaseService<BaseAgv>, IBaseAgvService
    {
        /// <summary>
        /// 查询agv信息列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<BaseAgvDto> GetPageList(BaseAgvQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<BaseAgv, BaseAgvDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="AgvId"></param>
        /// <returns></returns>
        public BaseAgv GetInfo(string AgvId)
        {
            var response = Queryable()
                .Where(x => x.AgvId == AgvId)
                .First();

            return response;
        }

        /// <summary>
        /// 添加agv信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public BaseAgv AddBaseAgv(BaseAgv model)
        {
            if (this.Any(it => it.AgvCode == model.AgvCode))
            {
                throw new CustomException($"系统中已包含{model.AgvCode}编码信息");
            }
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改agv信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateBaseAgv(BaseAgv model)
        {
            if (this.Any(it => it.AgvCode == model.AgvCode && it.AgvId != model.AgvId))
            {
                throw new CustomException($"系统中已包含{model.AgvCode}编码信息");
            }
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<BaseAgv> QueryExp(BaseAgvQueryDto parm)
        {
            var predicate = Expressionable.Create<BaseAgv>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.AgvCode), it => it.AgvCode == parm.AgvCode);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.StatusString), it => it.StatusString == parm.StatusString);
            return predicate;
        }
    }
}